package cn.glutnn.controller;


import cn.glutnn.entity.*;
import cn.glutnn.entity.vo.ShoppingcartVo;
import cn.glutnn.service.PurchaseDetailService;
import cn.glutnn.service.PurchaseService;
import cn.glutnn.service.SellPurchaseService;
import cn.glutnn.service.ShoppingcartService;
import cn.glutnn.utils.ThreadLocalUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * (Shoppingcart)表控制层
 *
 * @author msh
 * @since 2024-10-25 09:49:32
 */
@RestController
@RequestMapping("/shoppingcart")
@Tag(name = "购物车管理模块")
public class ShoppingcartController {
    /**
     * 服务对象
     */
    @Autowired
    private ShoppingcartService shoppingcartService;
    @Autowired
    private PurchaseService purchaseService;
    @Autowired
    private PurchaseDetailService purchaseDetailService;
    @Autowired
    private SellPurchaseService sellPurchaseService;

    @Operation(summary = "添加购物车")
    @PostMapping("/add/{id}")
    public Result add(@PathVariable Integer id) {
        shoppingcartService.add(id);
        return new Result<>(0, "添加成功", null);
    }

    @Operation(summary = "删除购物车")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        shoppingcartService.removeById(id);
        return new Result<>(0, "删除成功", null);
    }

    @Operation(summary = "查询购物车")
    @GetMapping("/select")
    public Result<List<ShoppingcartVo>> select() {
        return shoppingcartService.selectAllByOwnName();
    }

    @Operation(summary = "修改购物车数量")
    @PutMapping("/update/{id}/{count}")
    public Result<Shoppingcart> update(@PathVariable("id") Integer id, @PathVariable("count") Integer count) {
        Shoppingcart shoppingcart = new Shoppingcart(id, null, count, null, null, new Date());
        shoppingcartService.updateById(shoppingcart);
        return new Result<>(0, "修改成功", null);
    }

    @Operation(summary = "清空购物车")
    @DeleteMapping("/clear")
    public Result clear() {
        QueryWrapper<Shoppingcart> queryWrapper = new QueryWrapper();
        Map<String, Object> map = ThreadLocalUtil.get();
        queryWrapper.eq("own_name", map.get("username"));
        shoppingcartService.remove(queryWrapper);
        return new Result<>(0, "清空成功", null);
    }

    @Operation(summary = "提交订单")
    @PostMapping("/commitOrder/{addId}/{tMoney}")
    public Result commitOrder(@PathVariable("addId") Integer addId,
                              @PathVariable("tMoney") String tMoney,
                              @RequestBody List<ShoppingModel> shoppingModelList) {
        //获取当前用户
        Map<String, Object> map = ThreadLocalUtil.get();
        String name = (String) map.get("username");
        //添加购买人买入订单
        //创建Purchase实例，设置订单类型为
        Purchase purchase =
                new Purchase(null, name, 1, Double.parseDouble(tMoney), addId.toString(), 1, new Date(), new Date());
        //保存订单到数据库并返回id
        purchaseService.saveAndGetId(purchase);
        Integer purchaseId = purchase.getPurchaseId();
        System.out.println("purchaseId 数据库返回的id= " + purchaseId);
        //添加购买人买入订单详细
        if (shoppingModelList != null && !shoppingModelList.isEmpty()) {
            for (ShoppingModel shoppingModel : shoppingModelList) {
                PurchaseDetail purchaseDetail = new PurchaseDetail();
                SellPurchase sellPurchase = new SellPurchase();
                purchaseDetail.setCount(shoppingModel.getCount());
                purchaseDetail.setOrderId(shoppingModel.getOrderId());
                purchaseDetail.setPurchaseId(Math.toIntExact(purchaseId));
                purchaseDetail.setSumPrice(Double.parseDouble(shoppingModel.getPrice()) * shoppingModel.getCount());
                purchaseDetail.setUninPrice(Double.valueOf(shoppingModel.getPrice()));
                //添加到订单详情
                purchaseDetailService.save(purchaseDetail);

                //卖出订单
                //添加卖出方卖出订单
                //创建Purchase实例，设置订单类型为
                sellPurchase.setOrderId(shoppingModel.getOrderId());
                sellPurchase.setAddress(addId.toString());
                sellPurchase.setOwnName(shoppingModel.getOwnName());
                sellPurchase.setPurchaseId(Math.toIntExact(purchaseId));
                sellPurchase.setPurchaseStatus(1);
                sellPurchase.setPurchaseType(2);
                sellPurchase.setSumPrice(Double.parseDouble(shoppingModel.getPrice()) * shoppingModel.getCount());
                sellPurchase.setUninPricee(Double.valueOf(shoppingModel.getPrice()));
                sellPurchase.setCreateTime(new Date());
                sellPurchase.setUpdateTime(new Date());
                sellPurchaseService.save(sellPurchase);

                //删除购物车信息
                shoppingcartService.removeById(shoppingModel.getShoppingId());
            }
        }
        return new Result<>(0, "提交成功", null);
    }
}


